<?php
namespace sess;
use SessionHandler;
class session_files extends SessionHandler{
    //初始化
    public function __construct(){
      // ini_set('session.serialize_handler','php_serialize');
	  }

    /**
     * 打开Session
     * @access public
     * @param string $savePath
     * @param mixed $sessName
     */
    public function open($savePath, $sessName)
    {
        $this->savePath=$savePath;
        $this->sessName=$sessName;
		    return true;
    }

    /**
     * 关闭Session
     * @access public
     */
    public function close()
    {
        $this->gc(C('cookie.expire'));
        return true;
    }

    /**
     * 读取Session
     * @access public
     * @param string $sessID
     */
    public function read($session_id)
    {
        $filename=$this->savePath."/sess_".$session_id;
        if(is_file($filename)){
            //上次被访问的时间fileatime
            $content=file_get_contents($filename);
            return $content;
        }
        return '';
    }
    /**
     * 写入Session
     * @access public
     * @param string $sessID
     * @param String $sessData
     */
    public function write($session_id,$session_data)
    {
        if(!empty($session_data)){
          $filename=$this->savePath."/sess_".$session_id;
          file_put_contents($filename,$session_data);
        }
        return true;
    }

    /**
     * 删除Session
     * @access public
     * @param string $sessID
     */
    public function destroy($session_id)
    {
        $filename=$this->savePath."/sess_".$session_id;
        if(is_file($filename)){
            unlink($filename);
        }
        return false;
    }

    /**
     * Session 垃圾回收
     * @access public
     * @param string $sessMaxLifeTime
     */
    public function gc($sessMaxLifeTime)
    {
        return true;
    }

}
